package com.htdc.web.controller;

import com.htdc.web.bean.Company;
import com.htdc.web.bean.ProjectTb;
import com.htdc.web.service.CountService;
import com.htdc.web.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * author:zbc
 * date:2018/9/5
 * description:
 */
@Controller
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

//跳转添加页面
    @RequestMapping("/toAddProject")
    public String toAddProject(HttpSession session){
        List<Company> companies =projectService.getAllCompanies();
        session.setAttribute("companies",companies);
        return "add_project";
    }
    // 添加项目名称、所属公司、项目地址
    @RequestMapping(value = "/addProject",method = RequestMethod.POST)
    @ResponseBody
    public Object addProject(String pname,int cid, String address){
        projectService.addProject(pname, cid, address);
        return "success";
    }

    //查看项目
    @RequestMapping(value = "/getProject",method = RequestMethod.GET)
    public String getProject(int pid,HttpSession session){
            ProjectTb projectTb =projectService.getProject(pid);
            List<Company> companies =projectService.getAllCompanies();
            session.setAttribute("projectTb",projectTb);
            session.setAttribute("companies",companies);
            return "compile_company";
    }

    //编辑项目
    @RequestMapping(value = "/updateProject",method = RequestMethod.POST)
    @ResponseBody
    public Object updateProject(ProjectTb project){
        projectService.updateProject((int)project.getPid(),(int)project.getCid(), project.getPname(),project.getAddress());
        return "success";
    }

    //通过cid得到项目
    @RequestMapping("/getProjectByCid")
    public List<ProjectTb> getProjectByCid(int cid){
        List<ProjectTb> projectTbList = projectService.getProjectByCid(cid);
        return projectTbList;
    }

}
